package com.kirayous.admin.controller;


import com.kirayous.api.admin.service.IRoleService;
import com.kirayous.common.Result;
import com.kirayous.common.annotation.OptLog;
import com.kirayous.common.annotation.PagePathPatternsAno;
import com.kirayous.common.constant.OptTypeConst;
import com.kirayous.common.dto.UserRoleDTO;
import com.kirayous.common.entity.Role;
import com.kirayous.common.vo.ConditionVO;
import com.kirayous.common.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KiraYous
 * @since 2021-09-21
 */

@Api(tags = "角色模块")
@RestController
public class RoleController {
    @Autowired
    IRoleService roleService;


    @ApiOperation(value = "查询用户角色选项")
    @GetMapping("/admin/users/role")
    public Result listUserRole() {
        List <Role> role=roleService.listUserRoles();
       return Result.success().setMessage("查询成功").setData(role);
    }

    /**
     * 查询角色列表
     *
     * @param conditionVO 条件
     * @return  角色列表
     */
    @ApiOperation(value = "查询角色列表")
    @GetMapping("/admin/roles")
    @PagePathPatternsAno(path = "/admin/roles")
    public Result listRoles(ConditionVO conditionVO) {
        return Result.success().setData(roleService.listRoles(conditionVO));
    }

    /**
     * 保存或更新角色
     *
     * @param roleVO 角色信息
     * @return {@link Result<>}
     */
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    @ApiOperation(value = "保存或更新角色")
    @PostMapping("/admin/role")
    public Result saveOrUpdateRole(@RequestBody @Valid RoleVO roleVO) {
        roleService.saveOrUpdateRole(roleVO);
        return Result.success().setMessage("操作成功");
    }

    /**
     * 删除角色
     *
     * @param roleIdList 角色id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = OptTypeConst.REMOVE)
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/admin/roles")
    public Result deleteRoles(@RequestBody List<Integer> roleIdList) {
        roleService.deleteRoles(roleIdList);
        return Result.success();
    }

}

